package com.dounine.test.thread;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import javax.print.attribute.standard.MediaSize.NA;

import org.apache.http.Consts;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

public class SumThread extends Thread {

	private List<Host> ids = new ArrayList<>();
	private CloseableHttpClient httpClient;
	private HttpGet request;
	private int sum;

	public SumThread(String name, List<Host> ids,CloseableHttpClient httpClient,HttpGet request) {
		super(name);
		this.ids = ids;
		this.httpClient = httpClient;
		this.request = request;
	}

	@Override
	public void run() {
		for (Host host : ids) {
			if(!HostUtil.checkCollect(host.getHn(), host.getP()))continue;
			CloseableHttpResponse response = null;
			try {
				System.out.println(host.getHn());
				request.setConfig(RequestConfig.custom().setProxy(new HttpHost(host.getHn(),host.getP())).build());
				response = httpClient.execute(request);
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					response.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public List<Host> getIds() {
		return ids;
	}

	public void setIds(List<Host> ids) {
		this.ids = ids;
	}

	public int getSum() {
		return sum;
	}

	public void setSum(int sum) {
		this.sum = sum;
	}

}
